{% extends 'base.html' %}
{% load static %}

{% block title %}报表 - Allen监控系统{% endblock %}

{% block extra_css %}
<link href="{% static 'css/dataTables.bootstrap5.min.css' %}" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="container-fluid">
    <h2 class="mb-4">系统报表</h2>

    <!-- 报表类型选择 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">报表类型</label>
                    <select name="report_type" class="form-select" id="reportType">
                        <option value="asset_status" {% if report_type == 'asset_status' %}selected{% endif %}>资产状态统计</option>
                        <option value="alert_summary" {% if report_type == 'alert_summary' %}selected{% endif %}>告警统计</option>
                        <option value="device_status" {% if report_type == 'device_status' %}selected{% endif %}>设备状态统计</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">开始日期</label>
                    <input type="date" name="start_date" class="form-control" value="{{ start_date|date:'Y-m-d' }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">结束日期</label>
                    <input type="date" name="end_date" class="form-control" value="{{ end_date|date:'Y-m-d' }}">
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-primary w-100">生成报表</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 报表内容 -->
    <div class="card">
        <div class="card-body">
            <div class="d-flex justify-content-between mb-3">
                <h5 class="card-title">{{ report_title }}</h5>
                <button class="btn btn-success" onclick="exportToExcel()">
                    <i class="fas fa-file-excel"></i> 导出Excel
                </button>
            </div>

            {% if report_type == 'asset_status' %}
            <!-- 资产状态统计表 -->
            <div class="table-responsive">
                <table class="table table-bordered" id="reportTable">
                    <thead>
                        <tr>
                            <th>状态</th>
                            <th>数量</th>
                            <th>占比</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in report_data %}
                        <tr>
                            <td>{{ item.status_display }}</td>
                            <td>{{ item.count }}</td>
                            <td>{{ item.percentage }}%</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% elif report_type == 'alert_summary' %}
            <!-- 告警统计表 -->
            <div class="table-responsive">
                <table class="table table-bordered" id="reportTable">
                    <thead>
                        <tr>
                            <th>告警级别</th>
                            <th>数量</th>
                            <th>占比</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in report_data %}
                        <tr>
                            <td>{{ item.level_display }}</td>
                            <td>{{ item.count }}</td>
                            <td>{{ item.percentage }}%</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% elif report_type == 'device_status' %}
            <!-- 设备状态统计表 -->
            <div class="table-responsive">
                <table class="table table-bordered" id="reportTable">
                    <thead>
                        <tr>
                            <th>设备类型</th>
                            <th>在线数量</th>
                            <th>离线数量</th>
                            <th>总数</th>
                            <th>在线率</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for item in report_data %}
                        <tr>
                            <td>{{ item.device_type }}</td>
                            <td>{{ item.online_count }}</td>
                            <td>{{ item.offline_count }}</td>
                            <td>{{ item.total_count }}</td>
                            <td>{{ item.online_rate }}%</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% endif %}

            {% if report_data|length == 0 %}
            <div class="alert alert-info">
                暂无数据
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
<script src="{% static 'js/dataTables.bootstrap5.min.js' %}"></script>
<script src="https://cdn.jsdelivr.net/npm/xlsx@0.18.5/dist/xlsx.full.min.js"></script>
<script>
$(document).ready(function() {
    $('#reportTable').DataTable({
        "language": {
            "url": "{% static 'js/datatables-zh.json' %}"
        },
        "pageLength": 10,
        "ordering": true
    });
});

function exportToExcel() {
    const table = document.getElementById('reportTable');
    const wb = XLSX.utils.table_to_book(table, {sheet: "报表"});
    const reportType = document.getElementById('reportType').value;
    const date = new Date().toISOString().slice(0, 10);
    const fileName = `${reportType}_report_${date}.xlsx`;
    XLSX.writeFile(wb, fileName);
}
</script>
{% endblock %} 